cmake_minimum_required(VERSION 2.6)

# base common definitions
set(TargetName                  "bjdb")
set(ProjectName                 "bjdb")

##########################################################################
project(${ProjectName})

#set(CMAKE_BUILD_TYPE Release)

if(NOT BASE_CMAKEMOD_DIR)
  set(BASE_CMAKEMOD_DIR "../../basecomm")
endif()

include("${BASE_CMAKEMOD_DIR}/cmakemods/cmkutils.cmake")
include("${BASE_CMAKEMOD_DIR}/cmakemods/lnkslibc.cmake")


set(BJDB_SRC_NAMES  kcutil kcdb kcthread kcfile kccompress kccompare kcmap kcregex kcplantdb 
                    kcprotodb kcstashdb kccachedb kchashdb kcdirdb kcpolydb kcdbext kclangc 
   )

set(BJDB_UTIL_NAMES kcutiltest kcutilmgr kcprototest kcstashtest kccachetest kcgrasstest 
                    kchashtest kchashmgr kctreetest kctreemgr kcdirtest kcdirmgr kcforesttest kcforestmgr
                    kcpolytest kcpolymgr kclangctest
   )

set(BJDB_SRC )
foreach(FNAM ${BJDB_SRC_NAMES})
  foreach(FEXT "cc" "h" "c")
    set(FULL_FPATH "${CMAKE_CURRENT_LIST_DIR}/${FNAM}.${FEXT}")
    if(EXISTS ${FULL_FPATH})
      list(APPEND BJDB_SRC ${FULL_FPATH})
    endif()
  endforeach()
endforeach()

add_definitions(-D_CRT_SECURE_NO_WARNINGS)
include_directories(.)

if(MSVC)
  util_add_cflags("/wd4244" "/wd4351" "/wd4800")
endif()

add_library(${TargetName} STATIC ${BJDB_SRC})

function(BuildUtils utilnam)
  foreach(EXT cc c)
    set(FPATH ${CMAKE_CURRENT_LIST_DIR}/${utilnam}.${EXT})
    if(EXISTS ${FPATH})
      add_executable(${utilnam} ${FPATH}) 
      add_dependencies(${utilnam} ${TargetName})
      target_link_libraries(${utilnam} ${TargetName})
    endif()  
  endforeach()
endfunction()

foreach(NAM ${BJDB_UTIL_NAMES})
  BuildUtils(${NAM})
endforeach()

# windows default to c:/programm files
# unix default to /usr/local
#if(NOT CMAKE_INSTALL_PREFIX)
#  set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_LIST_DIR})
#endif()

set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_LIST_DIR})

# do installation of the project
install(TARGETS ${TargetName} EXPORT ${TargetName}
        RUNTIME DESTINATION ../bin
        LIBRARY DESTINATION ../lib
        ARCHIVE DESTINATION ../lib
  )

install(EXPORT ${TargetName} DESTINATION ../lib)
install(TARGETS ${BJDB_UTIL_NAMES}
        RUNTIME DESTINATION ../bin
        LIBRARY DESTINATION ../lib
        ARCHIVE DESTINATION ../lib
  )

file(GLOB INCLFILES . *.h)
list(REMOVE_ITEM INCLFILES ${CMAKE_CURRENT_LIST_DIR}/myconf.h ${CMAKE_CURRENT_LIST_DIR}/cmdcommon.h)
install(FILES ${INCLFILES} DESTINATION ../include)

install(DIRECTORY doc/ DESTINATION ../doc)

